

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

class CommonUtils {
  ///检查值是否为空
  static bool checkNotNull(dynamic value) {
    if (value == null) {
      return false;
    }

    if (value.runtimeType == bool) {
      return !value;
    }

    if (value.runtimeType == String) {
      return value != '';
    }

    return true;
  }
  ///动态计算文本 size
  static Size boundingTextSize(String text, TextStyle style,  {int maxLines = 2^31, double maxWidth = double.infinity}) {
    if (text.isEmpty) {
      return Size.zero;
    }
    final TextPainter textPainter = TextPainter(
        textDirection: TextDirection.ltr,
        text: TextSpan(text: text, style: style), maxLines: maxLines)
      ..layout(maxWidth: maxWidth);
    return textPainter.size;
  }
  ///复制到粘贴板
  static void copy2ClipBoard({required String copyTex}) {
    Clipboard.setData(ClipboardData(text: copyTex));
  }
  ///检查两个版本号的大小
  ///1 最新，0 需要更新
  ///限定格式 1.1.1
  static int checkVersion(String localVersion, String serverVersion) {
    List<String> localList = localVersion.split('.');
    List<String> serverList = serverVersion.split('.');

    int compare = 1;
    for (int i = 0; i < localList.length; i ++) {
      int localV = int.parse(localList[i]);
      int serverV = int.parse(serverList[i]);

      if (localV < serverV) {
        compare = 0;
        break;
      }
    }
    return compare;

  }

  static int lastMillisecond = 0;
  ///防抖函数
  static bool clickEnable() {
    int curMillisecond = DateTime.now().millisecondsSinceEpoch;
    bool isClickEnable = curMillisecond - lastMillisecond > 1000;
    lastMillisecond = curMillisecond;

    return isClickEnable;
  }

  ///网络监听
  static void connectivityListener(Function(bool) func) async {
    final List<ConnectivityResult> connectivityResult = await (Connectivity().checkConnectivity());

// This condition is for demo purposes only to explain every connection type.
// Use conditions which work for your requirements.
    if (connectivityResult.contains(ConnectivityResult.mobile)) {
      // Mobile network available.
      func(true);
    } else if (connectivityResult.contains(ConnectivityResult.wifi)) {
      // Wi-fi is available.
      // Note for Android:
      // When both mobile and Wi-Fi are turned on system will return Wi-Fi only as active network type
      func(true);
    } else if (connectivityResult.contains(ConnectivityResult.ethernet)) {
      // Ethernet connection available.
    } else if (connectivityResult.contains(ConnectivityResult.vpn)) {
      // Vpn connection active.
      // Note for iOS and macOS:
      // There is no separate network interface type for [vpn].
      // It returns [other] on any device (also simulator)
    } else if (connectivityResult.contains(ConnectivityResult.bluetooth)) {
      // Bluetooth connection available.
    } else if (connectivityResult.contains(ConnectivityResult.other)) {
      // Connected to a network which is not in the above mentioned networks.
    } else if (connectivityResult.contains(ConnectivityResult.none)) {
      // No available network types
    }
  }
}